#include "InfinatePlane.h"

bool InfinatePlane::intersect(const Ray &ray, IntersectResult &result) const {
	Double a = ray.dir * normal;
	if (ISZERO(a)) return false;
	if (!doubleSide && a > 0) return false;
	Double d = normal * (position - ray.origin) / a;
	if (d < EPS) return false;
	result.primitive = this;
	result.distance = d;
	result.position = ray.getPoint(d);
	result.normal = (a > 0) ? -normal : normal;
	return true;
}

bool InfinatePlane::intersectP(const Ray & ray) const {
	return (doubleSide || (ray.dir * normal < 0));
}

string InfinatePlane::str() const {
	std::ostringstream s;
	s << getName() << "(pos = " << position.str() << ", normal = " << normal.str() << ")";
	return s.str();
}